//添加未完成任务
Ext.namespace("MyPlan");

MyPlan.AddTask = function(p) {
	this.winParent = p;
	this.dtStart = new Ext.form.DateField({
		fieldLabel : '发起时间',
		name : 'taskDateFm',
		width : 85,
		format :'Y-m-d'
	});
	this.dtEnd=new Ext.form.DateField({
		fieldLabel:'～',
		name:'taskDateTo',
		width: 85,
		format:'Y-m-d'
	});
	this.btnSearch = new Ext.Toolbar.Button({
        iconCls: 'icon-search',
        text: '查找',
        tooltip: {text: '查找任务'},
        handler: this.queryTask,
        scope: this
    });
	this.store = new Ext.data.JsonStore({
	    url: Utils.formatURL('../myplan/searchTask.form?&timesnap=' + Date.parse(new Date())),
	    root: 'result',
	    fields: ['taskId', 'taskTitle','taskContent','taskDate','taskCompleteDate','taskDispatcher',
	    	'dispatcherName','taskType','taskOrigin','taskRemark','taskDifficulty','taskExeStatus','taskExeId']
	});
	this.renderSource = function(value){
		return OMS.TASKORIGIN[value];
	};
	this.renderCategory = function(value){
		return OMS.PLANCATEGORY[value];
	};
	this.renderStatus = function(value){
		return OMS.TASKSEXESTATUS[value];
	};
	this.renderContent=function(value){
		return Ext.util.Format.ellipsis(value,22,'...');
	}
	this.sm =  new Ext.grid.CheckboxSelectionModel();
	this.columns = [
		  this.sm,
		  {header: '任务编号', sortable: true, align: 'left',hidden:true, dataIndex: 'taskId'},	
		  {header: '任务明细编号', sortable: true, align: 'left',hidden:true, dataIndex: 'taskExeId'},		  
		  {header: '任务来源', sortable: true,width: 20, align: 'left',dataIndex: 'taskOrigin',renderer:this.renderSource},		  
 	      {header: '任务名称', sortable: true, width: 40, align: 'left', dataIndex: 'taskTitle'},
 	      {header: '任务类别', sortable: true,width: 20, align: 'left',dataIndex: 'taskType',renderer:this.renderCategory},
 	      {header: '内容描述', sortable: true, align: 'left', dataIndex: 'taskContent',renderer:this.renderContent},
 	      {header: '派发时间', sortable: true, align: 'center', width: 20,hidden:true, dataIndex: 'taskDate'},
 	      {header: '完成时间', sortable: true, align: 'center', width: 20, dataIndex: 'taskCompleteDate'},
 	      {header: '派发人ID', sortable: true, align: 'center', hidden:true,width: 40, dataIndex: 'taskDispatcher'},
 	      {header: '执行ID', sortable: true, align: 'center', hidden:true,width: 40, dataIndex: 'taskExecutor'},
 	      {header: '派发人', sortable: true, align: 'center', width: 20, dataIndex: 'dispatcherName'},
 	      {header: '重点、难点及需协调事宜', width: 40, hidden:true,align: 'left', dataIndex: 'taskDifficulty'},
 	      {header: '备注', sortable: true,width: 30,  hidden:true,align: 'left',dataIndex: 'taskRemark'}, 	      
 	      {header: '执行状态', sortable: true, align: 'center', width: 14, hidden:true,dataIndex: 'taskExeStatus',renderer:this.renderStatus}
 	];
	this.tbar=['->','发起时间:', this.dtStart, '~', this.dtEnd,this.btnSearch];
	this.bbar=new Ext.PagingToolbar({
		 pageSize: 15,
         store: this.store,
         displayInfo: true,
         displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
         emptyMsg: "没有记录..."
	});
	this.grid = new Ext.grid.GridPanel({
		store: this.store,
        region: 'center',
        cm: new Ext.grid.ColumnModel(this.columns),
        sm: this.sm,
        //tbar:this.tbar,
        bbar:this.bbar,
        viewConfig: { forceFit: true }
	});
	
	MyPlan.AddTask.superclass.constructor.call(this, {
		layout: 'border',
		title: '未完成任务',
        width: 800,
        height: 500,
        resizable: false,
        autoScroll: true,
        closeAction: 'hide',
        items: [this.grid],
		buttons:[{
			text: '添加任务到计划',
			cls: 'x-btn-text-icon btn-connect',
			handler: this.onAdd,
			scope: this
		}, {
            text: '关闭',
            cls: 'x-btn-text-icon btn-cancel',
            handler: this.hide.createDelegate(this, [])
        }]
    });
};
Ext.extend(MyPlan.AddTask, Ext.Window,{
	onAdd: function() {
		var records = this.grid.getSelectionModel().getSelections();
		if(records.length==0){
			Utils.warn('请选择一条计划！');
			return ;
		}
		/*for(var i=0;i<records.length;i++){
			var d=records[i].data;
			var pstore=this.winParent.storeItems;
			for(var j=0;j<pstore.getCount();j++){
				var pd = pstore.getAt(j).data;	
				if(pd['taskId']==d.taskId){
					Utils.warn('选中任务已存在计划中，请重新选择！');
					return;
				}
			}		
		}*/	
		for(var i=0;i<records.length;i++){
			var d=records[i].data;
			//alert(Ext.util.JSON.encode(d));
			var data={itemCategory:d.taskType,itemTitle:d.taskTitle,workSource:d.taskOrigin,
			itemContent:d.taskContent,planWork:d.taskDate,planFinish:d.taskCompleteDate,
			itemRemark:d.taskRemark,taskId:d.taskId,taskStatus:d.taskExeStatus,itemFm:d.taskExeId,sendFlag:'1'};			
			//alert(Ext.util.JSON.encode(data));
			var rec = new this.grid.store.recordType(data);
        	this.winParent.grid.store.insert(0, rec);
		}
		this.hide();
	},
	initWin:function(btn,event){
		this.store.removeAll();
		this.queryTask();
	},
	queryTask:function(btn,event){
		var param={
			taskDateFm:this.dtStart.getValue(),
			taskDateTo:this.dtEnd.getValue()
		};
		Utils.formatDate(param,'taskDateFm');
		Utils.formatDate(param,'taskDateTo');
		
		var ids='';
		var count=this.winParent.storeItems.getCount();
		for(var i=0 ;i<count;i++){
			var id = this.winParent.storeItems.getAt(i).data['taskId'];
			if(id) ids+=id+',';
		}
		param['taskIds']=ids;
		param['userId']=this.winParent.createId;
		//alert(Ext.util.JSON.encode(param));
		this.store.load({
			params:param
		});
	}
});